/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: InsertBatchSomeColumn
 * @Package com.rx.core.dbase
 * @Description: 批量插入处理函数
 * @author: 陈锦韬
 * @date: 2021/6/18
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.dbase;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import lombok.NoArgsConstructor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * @author: CHENJINTAO170
 * @Description: 批量更新处理函数
 * @date: 2021/6/18
 */
@NoArgsConstructor
//@AllArgsConstructor
@SuppressWarnings("serial")
public class UpdateBatchSomeColumn extends AbstractMethod {
    public static final String UPDATE_FORMAT = "<script>update %s %s where bid in %s and ver in %s</script>";
    public static final String UPDATE_BID_CONDITION = "<foreach collection=\"list\" index=\"index\" item=\"item\" separator=\",\" open=\"(\" close=\")\">\n" +
            "        #{item.bid,jdbcType=VARCHAR}\n" +
            "</foreach>";
    public static final String UPDATE_VER_CONDITION = "<foreach collection=\"list\" index=\"index\" item=\"item\" separator=\",\" open=\"(\" close=\")\">\n" +
            "        #{item.ver,jdbcType=BIGINT}\n" +
            "</foreach>";

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sql = UPDATE_FORMAT;

        String setSql = getSetSql(tableInfo);
        String bidCondtionSql = UPDATE_BID_CONDITION;
        String verCondtionSql = UPDATE_VER_CONDITION;
        String sqlResult = String.format(sql, tableInfo.getTableName(), setSql,bidCondtionSql,verCondtionSql);

        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
        return this.addUpdateMappedStatement(mapperClass, modelClass, "batchUpdateNoXml", sqlSource);
    }
    private boolean filter(TableFieldInfo tableFieldInfo){
        if (RxBaseDbMust.BID.equalsIgnoreCase(tableFieldInfo.getProperty())){
            return true;
        }

        if (RxBaseDbMust.ID.equalsIgnoreCase(tableFieldInfo.getProperty())){
            return true;
        }
        if (RxMarkDbMust.CREATE_BY_ID.equalsIgnoreCase(tableFieldInfo.getColumn())){
            return true;
        }
        if (RxMarkDbMust.CREATE_DATE.equalsIgnoreCase(tableFieldInfo.getColumn())){
            return true;
        }
        if (RxBizDbMust.CREATE_BY.equalsIgnoreCase(tableFieldInfo.getColumn())){
            return true;
        }
        if (RxBizDbMust.CREATE_ORG_BY.equalsIgnoreCase(tableFieldInfo.getColumn())){
            return true;
        }
        if (RxBizDbMust.CREATE_ORG_ID.equalsIgnoreCase(tableFieldInfo.getColumn())){
            return true;
        }
        if (RxBizDbMust.VALID.equalsIgnoreCase(tableFieldInfo.getColumn())){
            return true;
        }
        return false;
    }
    public String getSetSql(TableInfo tableInfo){
        StringBuilder valueSql = new StringBuilder();
        valueSql.append("<trim prefix=\"set\" suffixOverrides=\",\">");
        for (TableFieldInfo tableFieldInfo : tableInfo.getFieldList()) {
            if (filter(tableFieldInfo)){
                continue;
            }


            valueSql.append("<trim prefix=\"").append(tableFieldInfo.getColumn()).append(" =case\" suffix=\"end,\">");
            valueSql.append("<foreach collection=\"list\" item=\"item\" index=\"index\">");
            if (RxBaseDbMust.VER.equalsIgnoreCase(tableFieldInfo.getProperty())){
                valueSql.append("when bid=#{item.bid} and ver=#{item.ver} and valid='0' then ver + 1 ");
            }else {
                valueSql.append("<if test=\"item.").append(tableFieldInfo.getProperty()).append("!=null\">");
                valueSql.append("when bid=#{item.bid} and ver=#{item.ver} and valid='0' then #{item.").append(tableFieldInfo.getProperty()).append("} ");
                valueSql.append("</if>");
                valueSql.append("<if test=\"item.").append(tableFieldInfo.getProperty()).append("==null\">");
                valueSql.append("when bid=#{item.bid} and ver=#{item.ver} and valid='0' then ").append(tableInfo.getTableName()).append(".").append(tableFieldInfo.getColumn()).append(" ");
                valueSql.append("</if>");

            }

            valueSql.append("</foreach>");
            valueSql.append("</trim>");
        }

        valueSql.append("</trim>");
        return valueSql.toString();
    }

}
